package pri.damai.xiaowu.anydoor.business.group;

import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;
import lombok.Data;
import pri.damai.xiaowu.anydoor.constant.ChatConst;
import pri.damai.xiaowu.anydoor.business.BaseModel;
import pri.damai.xiaowu.anydoor.netty.ChannelStore;

import java.util.List;

/**
 * 加入群组
 * @Desc
 * @Author yxf
 * @Date 2022/7/27 20:50
 * 但行好事，莫问前程。
 */
@Data
public class GroupAddReq implements BaseModel {

    private String groupId;

    @Override
    public Byte code() {
        return 15;
    }

    @Override
    public void process(ChannelHandlerContext ctx) {
        List<Integer> userIds = ChannelStore.GROUP_USER_MAP.get(this.getGroupId());
        Object userId = ctx.channel().attr(AttributeKey.valueOf(ChatConst.USER_ID_KEY)).get();
        userIds.add((Integer) userId);

        GroupAddRsp rsp = new GroupAddRsp();
        rsp.setMsg("加入成功。当前群组成员:" + userIds);
        ctx.channel().writeAndFlush(rsp);
    }
}
